package edu.ntnu.controller;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Class represents map
 */
public class Map {

	public static final int SCALE = 5;
	public static final int MAX_WIDTH = 13000/SCALE;
	public static final int UNKNOWN = 0;
	public static final int WAY = -1;
	public static final int BALL = -2;
	public static final int LIGHT = -3;
	
	private int width;
	private int height;
	private int[][] map;
	
	public Map(int newWidth, int newHeight) {
		width = newWidth;
		height = newHeight;
		map = new int[width][height];	
		for(int x = 0; x < width; x++) {
			for(int y = 0; y < height; y++) {
				map[x][y] = UNKNOWN;
			}
		}
	}
	
	/**
	 * Copy constructor
	 * @param template	pattern of original object
	 */
	public Map(Map template) {
		width = template.width;
		height = template.height;
		map = new int[width][height];	
		for(int x = 0; x < width; x++) {
			for(int y = 0; y < height; y++) {
				map[x][y] = template.map[x][y];
			}
		}
	}

	/**
	 * Add information into map
	 * @param point	place in map where information should be stored
	 * @param value	information what is ther (WAY, BALL, LIGHT, UNKNOWN)
	 * @return		true if information has been stored, false otherwise
	 */
	public boolean add(Point point, int value) {
		int x = point.getX()/SCALE;
		int y = point.getY()/SCALE;
		if (x >= 0 && x < width && y >= 0 && y < height && (value < map[x][y] || value > 0)) {
			map[x][y] = value;
			return true;
		}
		return false;
	}
	
	/**
	 * Get information from map
	 * @param point	place in map where information should be stored
	 * @return		one of BALL, WAY, LIGHT or UNKNOWN
	 */
	public int get(Point point) {
		int x = point.getX()/SCALE;
		int y = point.getY()/SCALE;
		if (x >= 0 && x < width && y >= 0 && y < height) {
			return map[x][y];
		}
		return UNKNOWN;
	}
	
	/**
	 * Print out the map to bmp file.
	 * need only for testing
	 * @return	true if map has been saved correctly, false otherwise
	 */
	public boolean save() {
		System.out.print("saving map...");
		BufferedImage mapImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		int color;

		for(int x = 0; x < width; x++) {
			for(int y = 0; y < height; y++) {
				switch (map[x][y]) {
					case WAY:
						color = new Color(255,255,255).getRGB();
					break;
					case BALL:
						color = new Color(0,255,0).getRGB();
					break;
					case LIGHT:
						color = new Color(255,0,0).getRGB();
					break;
					case UNKNOWN:
						color = new Color(0,0,0).getRGB();
					break;
					default:
						color = new Color(map[x][y]/20,0,255 - map[x][y]/20).getRGB();
				}
				mapImage.setRGB(x, height - y - 1, color);
			}
			if (x % 300 == 0) {
				System.out.print(".");
			}
		}
		
		try {
			ImageIO.write(mapImage, "bmp", new File("map.bmp"));
			System.out.println("Map saved.");
			return true;
		} catch (IOException e) {
			  System.err.println("Error: " + e.getMessage());
			  return false;
		}
	}
}

